﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
namespace TestDxfDocument
{
    class FigureE1
    {
        static void OrdinateDimensionTest(string path)
        {
            DxfDocument dxf = new DxfDocument();
            //作图
            //参数
            double hf = 4.5;
            double h = 7.2;
            double pi = Math.PI;
            double par = 43 * pi / 180;
            //主视图
            Vector2 v1 = new Vector2(0, 0);
            Vector2 v2 = new Vector2(v1.X - ((h - hf) / 22), v1.Y + hf / 3.2);
            //
            Vector2 origin = new Vector2(0, 0);
            Vector2 refX = new Vector2(v1.X - ((h - hf) / 22), v1.Y + hf / 3.2);
            Vector2 refY = new Vector2(v2.X - hf / 30, v2.Y - hf / 200);
            double length = 3;
            double angle = 30;
            DimensionStyle myStyle = new DimensionStyle("CreateDimStyle");
            OrdinateDimension dimX1 = new OrdinateDimension(origin, refX, length, OrdinateDimensionAxis.X, 0, myStyle);
            OrdinateDimension dimX2 = new OrdinateDimension(origin, refX, length, OrdinateDimensionAxis.X, angle, myStyle);
            OrdinateDimension dimY1 = new OrdinateDimension(origin, refY, length, OrdinateDimensionAxis.Y, 0, myStyle);
            OrdinateDimension dimY2 = new OrdinateDimension(origin, refY, length, OrdinateDimensionAxis.Y, angle, myStyle);


            dxf.AddEntity(dimX1);
            dxf.AddEntity(dimY1);
            dxf.AddEntity(dimX2);
            dxf.AddEntity(dimY2);

            Line lineX = new Line(origin, origin + 5 * Vector2.UnitX);
            Line lineY = new Line(origin, origin + 5 * Vector2.UnitY);

            Vector2 point = Vector2.Polar(new Vector2(origin.X, origin.Y), 5, angle * MathHelper.DegToRad);
            Line lineXRotate = new Line(origin, new Vector2(v2.X, v2.Y));

            point = Vector2.Polar(new Vector2(origin.X, origin.Y), 5, angle * MathHelper.DegToRad + MathHelper.HalfPI);
            Line lineYRotate = new Line(origin, new Vector2(v1.X, v1.Y));
        }
        public static void ConverterR(Dictionary<string, Object> data)    //转换器,旋转对称

        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中心线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层
            //作图
            //参数
            double hf = 4.5;
            double oah = 8;
            double h = 7.2;
            double lh = 7.8;
            double pi = Math.PI;
            double par = 43 * pi / 180;
            double lk = 12;
            double f = 1;
            string path = Convert.ToString(data["path"]);
            //主视图
            Vector2 v1 = new Vector2(0, 0);
            Vector2 v2 = new Vector2(v1.X - ((h - hf) / 22), v1.Y + hf / 3.2);
            Vector2 v3 = new Vector2(v2.X - hf / 30, v2.Y - hf / 200);
            Vector2 v4 = new Vector2(v2.X + oah / 10.3, v2.Y + 0.1);
            Vector2 v5 = new Vector2(v1.X + lh / 6, v1.Y);
            Vector2 v6 = new Vector2(v5.X - hf / 5.8, v5.Y + (hf - hf / 2.25));
            Vector2 v7 = new Vector2(v3.X - 0.08, v3.Y + oah / 8.2);
            Vector2 v8 = new Vector2(v7.X + 0.28, v7.Y + (oah / 8) * (1.2 / 10));
            Vector2 v9 = new Vector2(v6.X + oah / 8, v6.Y + oah / 100);
            Vector2 v10 = new Vector2(v9.X + (oah / 8) / 9, v9.Y + Math.Tan(par) * (oah / 8) / 3.8);
            Vector2 v11 = new Vector2(v8.X + (oah / 8) * 9 / 10, v6.Y + 0.5);
            Vector2 v12 = new Vector2(v11.X + hf / 10, v11.Y + hf / 100);
            Vector2 v13 = new Vector2(v10.X + 0.0003, v10.Y + 0.0005);
            Vector2 v14 = new Vector2(v1.X + lk, v1.Y);
            Vector2 v15 = new Vector2(v13.X + 0.01, v13.Y - 0.1);
            Vector2 v16 = new Vector2(v15.X + 0.09, v15.Y - 0.18);
            Vector2 v17 = new Vector2(v16.X + 0.28, v16.Y);
            Vector2 v18 = new Vector2(v9.X + 0.21, v9.Y - 0.16);
            Vector2 v19 = new Vector2(v17.X, v18.Y);
            Vector2 v20 = new Vector2(v14.X, v19.Y);
            //
            Vector2 v21 = new Vector2(v1.X - oah / 16, v1.Y - oah / 2);
            Vector2 v22 = new Vector2(v7.X + 0.84, v21.Y - oah / 18);
            Vector2 v23 = new Vector2(v21.X + f * 1 / 5, v21.Y + f * 2);
            Vector2 v24 = new Vector2(v23.X + lk + f * 3 / 10, v23.Y);
            Vector2 v25 = new Vector2(v22.X, v22.Y + oah / 18);
            Vector2 v26 = new Vector2(v22.X, v22.Y + (oah / 18) * 1 / 2);
            Vector2 v27 = new Vector2(v26.X + lk - f / 2, v26.Y);
            Vector2 v28 = new Vector2(v25.X + f * 5.8 / 10, v25.Y - lk / 300);
            Vector2 v29 = new Vector2(v28.X + f + f * 1.8 / 10, v28.Y + f / 3);
            Vector2 v30 = new Vector2(v29.X + f / 10, v29.Y + f / 6);
            Vector2 v31 = new Vector2(v30.X - f * 11 / 50, v30.Y + f * 22 / 25);
            Vector2 v32 = new Vector2(v31.X - f * 1.5 / 10, v31.Y + f / 10);
            Vector2 v33 = new Vector2(v32.X - f - f / 10, v32.Y - f * 3 / 10);
            Vector2 v34 = new Vector2(v33.X - f * 2.1 / 5, v33.Y - f / 5);
            Vector2 v35 = new Vector2(v34.X - 0.32, v34.Y - 0.41);
            Vector2 v36 = new Vector2(v25.X - 0.36, v25.Y + 0.26);
            Vector2 v37 = new Vector2(v36.X + f + f / 5, v36.Y + f * 1.6 / 5);
            //
            Vector2 v38 = new Vector2(v29.X + f * 3 / 20, v29.Y - f / 2);
            Vector2 v39 = new Vector2(v38.X - f * 2 / 5, v38.Y + f * 2 / 5);
            Vector2 v40 = new Vector2(v38.X - f * 3 / 10, v38.Y);
            Vector2 v41 = new Vector2(v32.X - f * 3 / 25, v32.Y + f / 1.8);
            Vector2 v42 = new Vector2(v32.X - f * 2.6 / 10, v32.Y - f * 3 / 50);
            Vector2 v43 = new Vector2(v32.X - f * 2 / 5, v32.Y + f / 1.8);
            Vector2 v44 = new Vector2(v42.X - f - f / 5, v42.Y - f * 2 / 5);
            Vector2 v45 = new Vector2(v44.X - f * 4.6 / 5, v44.Y + f * 7 / 50);
            Vector2 v46 = new Vector2(v23.X + f * 3 + f / 25, v24.Y);
            Vector2 v47 = new Vector2(v22.X + f * 2 + f / 5, v22.Y - f * 9 / 10);
            //1
            Vector2 v48 = new Vector2(v43.X + f * 2 + f / 5, v46.Y - f * 9 / 10);
            Vector2 v49 = new Vector2(v48.X + oah / 8, v48.Y - f * 1.3);
            Vector2 v50 = new Vector2(v49.X + oah / 8, v49.Y - f * 1.3);
            Vector2 v51 = new Vector2(v50.X + f * 1 / 2, v50.Y + oah * Math.Log(par));
            Vector2 v52 = new Vector2(v51.X + f * 1 / 2, v51.Y + oah * Math.Log(par));
            Vector2 v53 = new Vector2(v51.X + f * 1 / 2, v51.Y + oah * Math.Log(par));
            Vector2 v54 = new Vector2(v51.X + f * 1 / 2, v51.Y + oah * Math.Log(par));
            Vector2 v55 = new Vector2(v54.X, v54.Y);
            Vector2 v56 = new Vector2(v55.X + Math.Tan(par) * Math.Sin(par), v55.Y);
            Vector2 v57 = new Vector2(v56.X, v56.Y - Math.Tan(par));
            Vector2 v58 = new Vector2(v57.X + Math.Round(par), v57.Y);
            Vector2 v59 = new Vector2(v58.X, v58.Y + Math.Round(par));
            // 线段
            //主视图
            Vector2[] Line1 = { v1, v2 };
            fun.drawBLine(dxf, Line1, 0.05, contour);
            Vector2[] Line2 = { v3, v2 };
            fun.drawBLine(dxf, Line2, 0.05, contour);
            Vector2[] Line3 = { v3, v4 };
            fun.drawBLine(dxf, Line3, 0.05, contour);
            Vector2[] Line4 = { v1, v5 };
            fun.drawBLine(dxf, Line4, 0.05, contour);
            Vector2[] Line5 = { v6, v5 };
            fun.drawBLine(dxf, Line5, 0.05, contour);
            Vector2[] Line6 = { v6, v4 };
            fun.drawBLine(dxf, Line6, 0.05, contour);
            Vector2[] Line7 = { v7, v3 };
            fun.drawBLine(dxf, Line7, 0.05, contour);
            Vector2[] Line8 = { v7, v6 };
            fun.drawBLine(dxf, Line8, 0.05, contour);
            Vector2[] Line9 = { v7, v8 };
            fun.drawBLine(dxf, Line9, 0.05, contour);
            Vector2[] Line10 = { v6, v9 };
            fun.drawBLine(dxf, Line10, 0.05, contour);
            //Vector2[] Line11 = { v10, v9 };
            //fun.drawBLine(dxf, Line11, 0.05, contour);
            LwPolyline Arc1 = new LwPolyline();
            Arc1.Vertexes.Add(new LwPolylineVertex(v8));
            Arc1.Vertexes.Add(new LwPolylineVertex(v11));
            Arc1.Vertexes[0].Bulge = -(oah / 8) * 11 / 50;
            fun.PolyFill(dxf, Arc1, 0.05, contour);
            Vector2[] Line12 = { v11, v12 };
            fun.drawBLine(dxf, Line12, 0.05, contour);
            LwPolyline Arc2 = new LwPolyline();
            Arc2.Vertexes.Add(new LwPolylineVertex(v9));
            Arc2.Vertexes.Add(new LwPolylineVertex(v10));
            Arc2.Vertexes[0].Bulge = (oah / 8) * 2 / 5;
            fun.PolyFill(dxf, Arc2, 0.05, contour);
            Vector2[] Line13 = { v10, v13 };
            fun.drawBLine(dxf, Line13, 0.05, contour);
            LwPolyline Arc3 = new LwPolyline();
            Arc3.Vertexes.Add(new LwPolylineVertex(v13));
            Arc3.Vertexes.Add(new LwPolylineVertex(v12));
            Arc3.Vertexes[0].Bulge = (oah / 8) * 1.88 / 5;
            fun.PolyFill(dxf, Arc3, 0.05, contour);
            Vector2[] Line14 = { v1, v14 };
            fun.drawBLine(dxf, Line14, 0.05, contour);
            Vector2[] Line15 = { v15, v16 };
            fun.drawBLine(dxf, Line15, 0.05, contour);
            Vector2[] Line16 = { v17, v16 };
            fun.drawBLine(dxf, Line16, 0.05, contour);
            Vector2[] Line17 = { v9, v18 };
            fun.drawBLine(dxf, Line17, 0.05, contour);
            Vector2[] Line18 = { v19, v18 };
            fun.drawBLine(dxf, Line18, 0.05, contour);
            Vector2[] Line19 = { v19, v20 };
            fun.drawBLine(dxf, Line19, 0.05, contour);
            Vector2[] Line20 = { v14, v20 };
            fun.drawBLine(dxf, Line20, 0.05, contour);
            Vector2[] Line21 = { v19, v17 };
            fun.drawBLine(dxf, Line21, 0.05, contour);
            //
            DimensionStyle dimensionStyle = fun.CreateDimStyle();
            dimensionStyle.ArrowSize = 0.1;
            dimensionStyle.DimArrow1 = DimensionArrowhead.Closedfilled;
            dimensionStyle.DimArrow2 = DimensionArrowhead.Closedfilled;
            dimensionStyle.TextHeight = 0.3;
            double offset = 6;
            LinearDimension dim1 = new LinearDimension(new Line(new Vector2(v20.X, v20.Y), new Vector2(v14.X, v14.Y)), offset / 2, 270, dimensionStyle);
            dim1.UserText = " H  ";
            dxf.AddEntity(dim1);
            LinearDimension dim2 = new LinearDimension(new Line(new Vector2(v19.X, v19.Y), new Vector2(v7.X, v7.Y)), offset / 2, 360, dimensionStyle);
            dim2.UserText = " LH  ";
            dxf.AddEntity(dim2);
            LinearDimension dim3 = new LinearDimension(new Line(new Vector2(v1.X, v1.Y), new Vector2(v7.X, v7.Y)), offset / 2, 90, dimensionStyle);
            dim3.UserText = " HF  ";
            dxf.AddEntity(dim3);
            LinearDimension dim4 = new LinearDimension(new Line(new Vector2(v1.X, v1.Y), new Vector2(v11.X, v11.Y)), offset + 1, 90, dimensionStyle);
            dim4.UserText = " OAH  ";
            dxf.AddEntity(dim4);
            //
            Vector2[] Line22 = { v21, v22 };
            fun.drawBLine(dxf, Line22, 0.05, contour);
            Vector2[] Line23 = { v23, v21 };
            fun.drawBLine(dxf, Line23, 0.05, contour);
            Vector2[] Line24 = { v23, v24 };
            fun.drawBLine(dxf, Line24, 0.05, contour);
            Vector2[] Line25 = { v25, v22 };
            fun.drawBLine(dxf, Line25, 0.05, contour);
            Vector2[] Line26 = { v26, v27 };
            fun.drawBLine(dxf, Line26, 0.05, contour);
            Vector2[] Line27 = { v24, v27 };
            fun.drawBLine(dxf, Line27, 0.05, contour);
            Vector2[] Line28 = { v25, v28 };
            fun.drawBLine(dxf, Line28, 0.05, contour);
            Vector2[] Line29 = { v29, v28 };
            fun.drawBLine(dxf, Line29, 0.05, contour);
            LwPolyline Arc4 = new LwPolyline();
            Arc4.Vertexes.Add(new LwPolylineVertex(v29));
            Arc4.Vertexes.Add(new LwPolylineVertex(v30));
            Arc4.Vertexes[0].Bulge = f / 2.2;
            fun.PolyFill(dxf, Arc4, 0.05, contour);
            Vector2[] Line30 = { v30, v31 };
            fun.drawBLine(dxf, Line30, 0.05, contour);
            LwPolyline Arc5 = new LwPolyline();
            Arc5.Vertexes.Add(new LwPolylineVertex(v31));
            Arc5.Vertexes.Add(new LwPolylineVertex(v32));
            Arc5.Vertexes[0].Bulge = f / 2.9;
            fun.PolyFill(dxf, Arc5, 0.05, contour);
            Vector2[] Line31 = { v32, v33 };
            fun.drawBLine(dxf, Line31, 0.05, contour);
            Vector2[] Line32 = { v33, v34 };
            fun.drawBLine(dxf, Line32, 0.05, contour);
            Vector2[] Line33 = { v35, v34 };
            fun.drawBLine(dxf, Line33, 0.05, contour);
            Vector2[] Line34 = { v36, v25 };
            fun.drawBLine(dxf, Line34, 0.05, contour);
            LwPolyline Arc6 = new LwPolyline();
            Arc6.Vertexes.Add(new LwPolylineVertex(v35));
            Arc6.Vertexes.Add(new LwPolylineVertex(v36));
            Arc6.Vertexes[0].Bulge = f / 3;
            fun.PolyFill(dxf, Arc6, 0.05, contour);
            LwPolyline poly = new LwPolyline();
            poly.Vertexes.Add(new LwPolylineVertex(fun.Polar(v37, 0, f / 3)));
            poly.Vertexes[0].Bulge = Math.Tan(Math.PI / 4);
            poly.Vertexes.Add(new LwPolylineVertex(fun.Polar(v37, Math.PI, f / 3)));
            poly.Vertexes[1].Bulge = Math.Tan(Math.PI / 4);
            poly.Vertexes.Add(new LwPolylineVertex(fun.Polar(v37, 0, f / 3)));
            fun.PolyFill(dxf, poly, 0.02, contour);
            LwPolyline poly1 = new LwPolyline();
            poly1.Vertexes.Add(new LwPolylineVertex(fun.Polar(v37, 0, f / 5.6)));
            poly1.Vertexes[0].Bulge = Math.Tan(Math.PI / 4);
            poly1.Vertexes.Add(new LwPolylineVertex(fun.Polar(v37, Math.PI, f / 5.6)));
            poly1.Vertexes[1].Bulge = Math.Tan(Math.PI / 4);
            poly1.Vertexes.Add(new LwPolylineVertex(fun.Polar(v37, 0, f / 5.6)));
            fun.PolyFill(dxf, poly1, 0.02, contour);
            Vector2[] Line35 = { v29, v38 };
            fun.drawBLine(dxf, Line35, 0.006, skdimensioning);
            Vector2[] Line36 = { v40, v39 };
            fun.drawBLine(dxf, Line36, 0.006, skdimensioning);
            Vector2[] Line37 = { v41, v32 };
            fun.drawBLine(dxf, Line37, 0.006, skdimensioning);
            Vector2[] Line38 = { v42, v43 };
            fun.drawBLine(dxf, Line38, 0.006, skdimensioning);
            Vector2[] Line39 = { v45, v44 };
            fun.drawBLine(dxf, Line39, 0.006, skdimensioning);
            Vector2[] Line40 = { v22, v47 };
            fun.drawBLine(dxf, Line40, 0.002, skdimensioning);
            //
            LinearDimension dim5 = new LinearDimension(new Line(new Vector2(v24.X, v24.Y), new Vector2(v27.X, v27.Y)), offset / 2, 270, dimensionStyle);
            dim5.UserText = " B  ";
            dxf.AddEntity(dim5);
            LinearDimension dim6 = new LinearDimension(new Line(new Vector2(v21.X, v21.Y), new Vector2(v23.X, v23.Y)), offset - 1, 90, dimensionStyle);
            dim6.UserText = " WF  ";
            dxf.AddEntity(dim6);
            LinearDimension dim7 = new LinearDimension(new Line(new Vector2(v22.X, v22.Y), new Vector2(v21.X, v21.Y)), offset - 1, 90, dimensionStyle);
            dim7.UserText = " WFA  ";
            dxf.AddEntity(dim7);
            LinearDimension dim8 = new LinearDimension(new Line(new Vector2(v27.X, v27.Y), new Vector2(v21.X, v21.Y)), offset + 1, 180, dimensionStyle);
            dim8.UserText = " LF  ";
            dxf.AddEntity(dim8);
            LinearDimension dim9 = new LinearDimension(new Line(new Vector2(v46.X, v46.Y), new Vector2(v27.X, v27.Y)), offset / 1.2, 180, dimensionStyle);
            dim9.UserText = " LS  ";
            dxf.AddEntity(dim9);
            Angular2LineDimension angdim1 = new Angular2LineDimension(new Line(v47, v21), new Line(v23, v21), offset / 3, dimensionStyle);
            //angdim1.UserText = "KAPR";
            //dxf.AddEntity(angdim1);
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }
    }
}
